#include<bits/stdc++.h>
#define sd(n) scanf("%d",&n) 
#define sld(n) scanf("%lld",&n)
#define pd(n) printf("%d", (n))
#define pld(n) printf("%lld", n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fi first
#define se second
const int N = 1e5 + 10;
#define INF 0x7fffffff
typedef long long int ll;
using namespace std;
//----------------------------------------------------------------------------//
char a[N];
int n, m;
bool st[26];
void solve()
{
	memset(st, 0, sizeof st);
	sd(n);
	//cin>>n;
	/*for (int i = 1; i <= n; i++) scanf("%c", &a[i]);*/
	scanf("%s", a + 1);
	sd(m);
	//cin>>m;
	for (int i = 1; i <= m; i++)
	{
		char ch[2]; //cin >> ch;
		scanf("%s", ch);
		st[ch[0] - 'a'] = true;
	}
	int ans = 0, last_letter = 0;
	for (int i = n; i > 0; i--)
	{
		if (last_letter) ans = max(ans, last_letter - i);
		if (st[a[i] - 'a']) last_letter = i;
	}
	//pd(ans);
	//puts("");
	//cout<<ans<<"\n";
	printf("%d\n", ans);


}

int main()
{

	int T;
	sd(T);
	// cin>>T;
	while (T--)
	{
		solve();
	}
	return 0;
}